package com.net.shop.car.manager.ui.personalcenter.jifen;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.net.shop.car.manager.App;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.JifenGood;
import com.net.shop.car.manager.api.model.JifenGoodDetail;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.api.volley.Response;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.jifen.GetJifenGoodDetail;
import com.net.shop.car.manager.api.volley.request.jifen.JifenToOilCard;
import com.net.shop.car.manager.api.volley.request.jifen.JifenToService;
import com.net.shop.car.manager.api.volley.response.jifen.JifenGoodDetailResponse;
import com.net.shop.car.manager.utils.Constants;

public class JiefenDetailView {

	private TextView goodName;
	private ImageView goodLogo;
	private TextView vbRequested;
	private ListView tels;
	private View accountInput;
	private TextView des;
	private TextView dangqianVb;
	private TextView kouchuVb;
	private TextView jiesuanBtn;
	private EditText account;
	private EditText oilCard;
	private JiFenDuiHuanDialog dialog;
	private BaseActivity activity;
	private Member user;

	/**
	 * @param activity
	 *            BaseActivity
	 * @param view
	 *            View
	 */
	public JiefenDetailView(JiFenDuiHuanDialog dialog,Member user, View view) {
		this.dialog = dialog;
		this.activity = dialog.activity;
		this.user = user;
		goodName = (TextView) view.findViewById(R.id.jifen_detail_goodname);
		goodLogo = (ImageView) view.findViewById(R.id.jifen_detail_iv);
		vbRequested = (TextView) view.findViewById(R.id.jifen_detail_vb);
		des = (TextView) view.findViewById(R.id.jifen_detail_des);
		tels = (ListView) view.findViewById(R.id.jifen_detail_tels);
		accountInput = view.findViewById(R.id.jifen_detail_input);
		account = (EditText) view.findViewById(R.id.jifen_detail_input_account);
		oilCard = (EditText) view.findViewById(R.id.jifen_detail_input_oilnum);
		dangqianVb = (TextView) view.findViewById(R.id.jifen_jiesuan_dangqian);
		jiesuanBtn = (TextView) view.findViewById(R.id.jifen_jiesuan_pay_btn);
		kouchuVb = (TextView) view.findViewById(R.id.jifen_jiesuan_kouchu);
	}

	/**
	 * @param good
	 *            JifenGood
	 * @param type
	 *            int
	 */
	public void setData(final JifenGood good, final int type) {
		String typeStr;
		if (type == 0) {
			tels.setVisibility(View.GONE);
			accountInput.setVisibility(View.VISIBLE);
			typeStr = Constants.JIFEN_DETAIL_OILCARD;
		} else {
			accountInput.setVisibility(View.GONE);
			tels.setVisibility(View.VISIBLE);
			typeStr = Constants.JIFEN_DETAIL_SERVICE;
		}
		final JifenGoodDetail goodDetail = good.getDetail();
		if (goodDetail != null) {
			goodName.setText(goodDetail.getGoodName());
			if (goodDetail.getGoodBitmap() == null) {
				if (good.getGoodBitmap() != null) {
					goodDetail.setGoodBitmap(good.getGoodBitmap());
					goodLogo.setImageBitmap(goodDetail.getGoodBitmap());
				} else {
					VolleyCenter.getVolley().addImageRequest(
							goodDetail.getGoodLogo(), new Listener<Bitmap>() {

								@Override
								public void onResponse(Bitmap response) {
									goodDetail.setGoodBitmap(good
											.getGoodBitmap());
									goodLogo.setImageBitmap(goodDetail
											.getGoodBitmap());
								}
							}, new ErrorListener() {

								@Override
								public void onErrorResponse(VolleyError error) {
									
								}
							});
				}
			} else {
				goodLogo.setImageBitmap(goodDetail.getGoodBitmap());
			}
			vbRequested.setText(goodDetail.getPointsRequested() + "");
			des.setText(goodDetail.getGoodDes());
			dangqianVb.setText(goodDetail.getMemeberVb() + "");
			kouchuVb.setText(goodDetail.getPointsRequested() + "");
			jiesuanBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					jiesuan(goodDetail, type);
				}
			});
		} else {
			VolleyCenter
					.getVolley()
					.addGetRequest(
							new GetJifenGoodDetail(
									typeStr,
									user.getMemberId(),
									good.getGoodId()),
							new VolleyListenerImpl<JifenGoodDetailResponse>(
									new JifenGoodDetailResponse(typeStr)) {

								@Override
								public void deleveryResponse(
										JifenGoodDetailResponse response) {
									if(response.isSuccess())  {
										JifenGoodDetail detail = response
												.getJifenGoodDetail();
										good.setDetail(detail);
										setData(good, type);
									}
								}
							});
		}
	}

	/**
	 * @param goodDetail JifenGoodDetail
	 * @param type final
	 */
	private void jiesuan(final JifenGoodDetail goodDetail, final int type) {
		if(type == 0) {
			String accoutStr = account.getText().toString().trim();
			if (TextUtils.isEmpty(accoutStr)) {
				App.i().showToast("请输入账号！");
				return;
			}
			String oilCardStr = oilCard.getText().toString().trim();
			if (TextUtils.isEmpty(oilCardStr)) {
				App.i().showToast("请输入油卡号！");
				return;
			}
			activity.progressDialog.show();
			VolleyCenter.getVolley().addGetRequest(new JifenToOilCard(user.getMemberId(),
					oilCardStr, accoutStr, goodDetail.getPointsRequested()), 
					new VolleyListenerImpl<Response>(new Response(Constants.JIFEN_TO_OILCARD)) {

						@Override
						public void deleveryResponse(Response response) {
							activity.progressDialog.dismiss();
							if (response.isSuccess()) {
								dialog.onBackPressed();
								App.i().showToast("油卡充值成功！");
							}
						}
					});
		} else {
			activity.progressDialog.show();
			VolleyCenter.getVolley().addGetRequest(new JifenToService(user.getMemberId(), 
					goodDetail.getSellerId(), goodDetail.getGoodId(), goodDetail.getGoodName(), 
					goodDetail.getPointsRequested()), new VolleyListenerImpl<Response>(new Response(Constants.JIFEN_TO_OILCARD)) {

						@Override
						public void deleveryResponse(Response response) {
							activity.progressDialog.dismiss();
							if (response.isSuccess()) {
								dialog.onBackPressed();
								App.i().showToast("积分兑换服务成功！");
							}
						}
					});
		}
	}
}
